
package logicBusiness;

import DAO.AulaDao;
import models.Aula;
import java.util.List;
import javax.persistence.*;


public class AulaBL {

    private int ok = 0;

    public int crearAula(String nombreAula, Integer capacidadAula, Integer disponibleAula) {

        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        em.getTransaction().begin();
        Aula a = dao.crearAula(nombreAula, capacidadAula, disponibleAula);
        em.getTransaction().commit();
        em.close(); 
        emf.close();
        if (a == null) {
            ok = 0;
        } else if (a.getNombreAula().equals(nombreAula)) {
            ok = 1;
        }
        return ok;

    }

    public void borrarAula(Integer idAula) {
        // find an user
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        em.getTransaction().begin();
        dao.borrarAula(idAula);
        em.getTransaction().commit();
        em.close(); 
        emf.close();

    }

    public int encontrarAula(Integer idAula) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        Aula a = dao.encontrarAula(idAula);
        em.close(); 
        emf.close();
        if (a == null) {
            ok = 0;
        } else if (a.getIdAula().equals(idAula)) {
            ok = 1;
        }
        return ok;

    }
public int encontrarAula(String nombreAula) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        Aula a = dao.encontrarAula(nombreAula);
        em.close(); 
        emf.close();
        if (a == null) {
            ok = 0;
        } else if (a.getNombreAula().equals(nombreAula)) {
            ok = a.getIdAula();
        }
        return ok;

    }

    public Aula devolverAula(Integer idAula) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        Aula a = dao.encontrarAula(idAula);
        return a;
    }

    public Aula consultarAula(Integer idAula) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        Aula a = dao.encontrarAula(idAula);
        em.close(); 
        emf.close();
        return a;
    }

    public int actualizarAula(Integer idAula, String nombreAula, Integer capacidadAula, Integer disponibleAula) {

        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        em.getTransaction().begin();
        Aula a = dao.actualizarAula(idAula, nombreAula, capacidadAula, disponibleAula);
        em.getTransaction().commit();
        em.close();
        emf.close();
        if (a == null) {
            ok = 0;
        } else if (a.getIdAula().equals(idAula)) {
            ok = 1;
        }
        return ok;
    }

    public List consultarAulas() {

        List results;
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        results = dao.encontrarAulas();
        em.close(); //quitar cuando se tenga montado el serv
        emf.close();//quitar cuando se tenga montado el serv
        return results;
    }

    public Integer prueba() {
        int i = 0;
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        AulaDao dao = new AulaDao(em);
        return i=dao.devolverId();
    }

    public void persist(Object object) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("sischoolPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
}
